package com.ruoyi.kjy.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.kjy.domain.ExitApplicationForm;
import com.ruoyi.kjy.domain.ExitCreateSpaceForm;
import com.ruoyi.kjy.service.ExitApplicationFormService;
import com.ruoyi.kjy.service.ExitCreateSpaceFormService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Objects;

/**
 * @Description:
 * @Author: Hi，Wang
 * @Date: 1/10,0010 17:50
 */


@Slf4j
@RestController
@RequestMapping("/exitApplicationForm")
public class ExitApplicationFormController extends BaseController {



    @Autowired
    private ExitApplicationFormService exitApplicationFormService;




    @GetMapping("/list")
    public AjaxResult list(Long current, Long size, ExitApplicationForm exitApplicationForm) {
        QueryWrapper<ExitApplicationForm> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(exitApplicationForm);
        if (Objects.isNull(current)) {
            current = 1l;
        }
        if (Objects.isNull(size)) {
            size = 10l;
        }
        if (size >= 20l) {
            size = 20l;
        }
        return AjaxResult.success(exitApplicationFormService.page(new Page<>(current, size), queryWrapper));
    }



    @GetMapping("/listByUserId")
    public AjaxResult listByUserId(Long current, Long size, ExitApplicationForm exitApplicationForm) {
        QueryWrapper<ExitApplicationForm> queryWrapper = new QueryWrapper<>();
        exitApplicationForm.setCreateBy(SecurityUtils.getUsername());
        queryWrapper.setEntity(exitApplicationForm);
        if (Objects.isNull(current)) {
            current = 1l;
        }
        if (Objects.isNull(size)) {
            size = 10l;
        }
        if (size >= 20l) {
            size = 20l;
        }
        return AjaxResult.success(exitApplicationFormService.page(new Page<>(current, size), queryWrapper));
    }





    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(exitApplicationFormService.getById(id));
    }

    @GetMapping(value = "/my")
    public AjaxResult getInfo() {
        LambdaQueryWrapper<ExitApplicationForm> eq = new QueryWrapper<ExitApplicationForm>().lambda().eq(ExitApplicationForm::getUserId, SecurityUtils.getUserId());
        eq.orderByDesc(ExitApplicationForm::getUpdateTime  );
        return AjaxResult.success(exitApplicationFormService.list(eq));
    }


    @Log(title = "离开kjy申请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ExitApplicationForm exitApplicationForm) {
        exitApplicationForm.setUserId(SecurityUtils.getUserId());
        exitApplicationForm.setStatus(0);
        boolean save = exitApplicationFormService.save(exitApplicationForm);
        return toAjax(save);
    }

    @Log(title = "离开kjy申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ExitApplicationForm exitApplicationForm) {

        return toAjax(exitApplicationFormService.updateById(exitApplicationForm));
    }

    @Log(title = "离开kjy申请", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable(value = "ids") Long[] ids) {
        return toAjax(exitApplicationFormService.removeByIds(Arrays.asList(ids)));
    }
}
